export async function compressImage(file, quality = 1, limitSize = 1.5) {
    return new Promise((resolve, reject) => {
        const name = file.file.name;
        const type = file.file.type;
        // Filter image regular expressions (compressed in format: jpg, jpeg)
        const imageFlag = /\/(?:jpg|jpeg)/i.test(type);
        // Convert units to bytes
        const transformSize = limitSize * 1024 * 1024;
        if (imageFlag && file.file.size > transformSize) {
            const canvas = document.createElement('canvas')
            const context = canvas.getContext('2d')
            let img = new Image()
            img.src = file.content
            img.onload = () => {
                const width = img.width;
                const height = img.height;
                canvas.width = width;
                canvas.height = height;
                context.drawImage(img, 0, 0, width, height);
                canvas.toBlob((compressedBlob) => {
                    file.file = new File([compressedBlob], name, { type, lastModified: Date.now() });
                    resolve(file);
                }, type, quality);
            }
        } else {
            // Save and return without processing
            resolve(file);
        }
    })
}